/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Game;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.net.URL;

/**
 *
 * @author VO CHI DUNG
 */
public class MyCard
{
    private int NumberCard;
    private int TypeCard;
    private Point Position;
    private Point BeginPosition;
    private Point EndPosition;
    private Image CardImage;
    private int State;      // 0: sleep, 1: hover, 2: select to play


    public MyCard(int num, int type, Point pos)
    {
        NumberCard = num;
        TypeCard = type;
        Position = pos;
        BeginPosition = pos;
        EndPosition = pos;
        CardImage = AutoChooseCardImage();
        State = 0;
    }

    public MyCard()
    {
        NumberCard = 0;
        TypeCard = 0;
        Position = null;
        CardImage = AutoChooseCardImage();
        State = 0;
    }

    private Image AutoChooseCardImage()
    {
        String cardType = "";

        switch (TypeCard)
        {
            case 0:
                cardType = "Heart";
                break;
            case 1:
                cardType = "Diamond";
                break;
            case 2:
                cardType = "Club";
                break;
            case 3:
                cardType = "Spade";
                break;
        }
        int indexImageCard = NumberCard;
        if(indexImageCard==14)
            indexImageCard = 1;
        String nameCard = "Resource/" + cardType + "Cards/" +
                            cardType + "_" + indexImageCard + ".png";
        ClassLoader loader = this.getClass().getClassLoader();
        URL cardPath = loader.getResource(nameCard);
        Image img = Toolkit.getDefaultToolkit().getImage(cardPath);
        
        Image scaleImage = img.getScaledInstance( GlobalSettings.CardWidth, GlobalSettings.CardHeight, Image.SCALE_SMOOTH);
        img = scaleImage;

        return img;
    }

    

    /**
     * @return the NumberCard
     */
    public int getNumberCard() {
        return NumberCard;
    }

    /**
     * @param NumberCard the NumberCard to set
     */
    public void setNumberCard(int NumberCard) {
        this.NumberCard = NumberCard;
    }

    /**
     * @return the TypeCard
     */
    public int getTypeCard() {
        return TypeCard;
    }

    /**
     * @param TypeCard the TypeCard to set
     */
    public void setTypeCard(int TypeCard) {
        this.TypeCard = TypeCard;
    }

    /**
     * @return the image
     */
    public Image getCardImage() {
        return CardImage;
    }

    /**
     * @return the State
     */
    public int getState() {
        return State;
    }

    /**
     * @param State the State to set
     */
    public void setState(int State) {
        this.State = State;
    }

    public void changeBackCardImage()
    {
        String nameCard = "Resource/BackCards/BackCard1.png";
        ClassLoader loader = this.getClass().getClassLoader();
        URL cardPath = loader.getResource(nameCard);
        CardImage = Toolkit.getDefaultToolkit().getImage(cardPath);

        Image scaleImage = CardImage.getScaledInstance(GlobalSettings.CardWidth, GlobalSettings.CardHeight, Image.SCALE_SMOOTH);
        CardImage = scaleImage;
    }

    public void processCursor(int x, int y, int with, int height)
    {
        if(Position.x < x && x < with + Position.x &&
           Position.y < y && y < height + Position.y)
        {
            State = 1;
            //return true;
        }
         else
        State = 0;
        //return false;
    }

    public void processCursor(int x, int y)
    {
        if(Position.x < x && x < GlobalSettings.CardWidth + Position.x &&
           Position.y < y && y < GlobalSettings.CardHeight + Position.y)
        {
            State = 1;
        }
        else
            State = 0;
    }

    public void processCursorOnLastCard(int x, int y)
    {
        if (getPosition().x < x && x < getPosition().x + GlobalSettings.CardWidth &&
            getPosition().y < y && y < getPosition().y + GlobalSettings.CardHeight)
        {
            State = 1;
        }
        else
            State = 0;
    }
    /**
     * Update MyCard
     */
    public void Update()
    {
        Point p = getPosition();
        Dimension d = GlobalSettings.getTransfer(BeginPosition, EndPosition);

        if (GlobalSettings.getDistance(BeginPosition, Position) < GlobalSettings.getDistance(BeginPosition, EndPosition))
        {
            int newX = 0;
            int newY = 0;
            if (d.width != 0)
            {
                // thay doi x, tinh y theo phuong trinh duong thang giua beginPos va endPos
                newX = p.x + d.width;
                newY = GlobalSettings.getYInLine(BeginPosition, EndPosition, newX);
                if (Math.abs(newX - BeginPosition.x) >= Math.abs(EndPosition.x - BeginPosition.x))
                {
                    newX = EndPosition.x;
                    newY = EndPosition.y;
                }
            }
            else
            {
                newX = p.x;
                newY = p.y + d.height;
                if (Math.abs(newY - BeginPosition.y) >= Math.abs(EndPosition.y - BeginPosition.y))
                {
                    newX = EndPosition.x;
                    newY = EndPosition.y;
                }
            }

            Position.x = newX;
            Position.y = newY;
            //repaint();
        }
    }

    /**
     * Draw MyCard
     */
    public void Draw(Graphics g)
    {
        Graphics2D g2D = (Graphics2D)g;
        //g2D.drawString(Integer.toString(Position.x) + "," + Integer.toString(Position.y), Position.x, Position.y);
        
        if (State == 0)
        g2D.drawImage(CardImage, getPosition().x, getPosition().y, null);
        else
        {
            if (State == 1)
             g2D.drawImage(CardImage, getPosition().x, getPosition().y - 20, null);
        }

        //g2D.drawString(Integer.toString(Position.x) + "," + Integer.toString(GlobalSettings.CardHeight + Position.y), Position.x, GlobalSettings.CardHeight + Position.y);
    }

    /**
     * @return the Position
     */
    public Point getPosition() {
        return Position;
    }

    /**
     * @param Position the Position to set
     */
    public void setPosition(Point Position) {
        this.Position = Position;
    }

    /**
     * @return the BeginPosition
     */
    public Point getBeginPosition() {
        return BeginPosition;
    }

    /**
     * @param BeginPosition the BeginPosition to set
     */
    public void setBeginPosition(Point BeginPosition) {
        this.BeginPosition = BeginPosition;
    }

    /**
     * @return the EndPosition
     */
    public Point getEndPosition() {
        return EndPosition;
    }

    /**
     * @param EndPosition the EndPosition to set
     */
    public void setEndPosition(Point EndPosition) {
        this.EndPosition = EndPosition;
    }


    public boolean equalCard(MyCard card)
    {
        if(NumberCard==card.getNumberCard()&&TypeCard==card.getTypeCard())
        {
            return true;
        }
        return false;
    }

    public boolean lessCard(MyCard card) {
        if (NumberCard < card.getNumberCard() && TypeCard == card.getTypeCard()) {
            return true;
        } else if (TypeCard < card.getTypeCard()) {
            return true;
        }
        return false;
    }

    public void setNumberAndType(MyCard card)
    {
        NumberCard = card.getNumberCard();
        TypeCard = card.getTypeCard();
    }
}
